<?php

namespace app\commonapi\controller;

use think\Controller;
use think\File;
use app\common\Api\UploadApi;

class Upload extends Controller {

  /**
   * showdoc
   * @catalog API文档/公共相关
   * @title 上传单张图片
   * @description 
   * @method POST
   * @url /commonapi/upload/uploadImg
   * @param file 必选 string 二进制文件 
   * @return {"code":1000,"data":{"all_url":"https://mmmx.cqthesis.cn/uploads/images/20190318/5237ee613a5d2750f7401b3660bf9f33.png","url":"20190318/5237ee613a5d2750f7401b3660bf9f33.png"},"msg":"上传成功"}
   * @return_param all_url string 全路径
   * @return_param url string 部分路径
   * @remark 
   * @number 0
   * @author lcx
   * @date 2018-11-6
   */
  public function uploadImg() {
    $way = config('pic_upload_way'); //1本地服务器上传 2七牛云上传
    if ($way == 2) {
      $result = $this->qiniuUploadImage();
    } else {
      $result = $this->commonUploadImage();
    }
    return json($result);
  }

  /*   * *七牛上传** */

  public function qiniuUploadImage($file = 'file') {
    $image = new UploadApi ();
    $res = $image->up2QiNiuYun($file);
    if ($res['code'] == 1) {
      $data['url'] = $res['data'];
      $data['all_url'] = UploadImgPath($res['data']);
      returnJson(1000, '上传成功', $data);
    } else {
      returnJson(-1000, '上传失败');
    }
  }

  /*   * * 上传到服务器* */

  public function commonUploadImage($filename = 'file') {
    $file = request()->file($filename);
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
    if ($info) {
      $data['url'] = $info->getSaveName();
      $data['all_url'] = UploadImgPath($info->getSaveName());
      returnJson(1000, '上传成功', $data);
    } else {
      returnJson(-1000, '上传失败');
    }
  }

}
